package me.keiwu.admin.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import me.keiwu.admin.domain.user.UserLabel;
import me.keiwu.admin.rpc.UserRpc;
import me.keiwu.admin.service.UserLabelService;
import me.keiwu.admin.vo.UserLabelSearch;
import me.keiwu.pub.data.Response;
import me.keiwu.pub.data.service.UserInfoBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户标签
 */
@RequestMapping("/api/user_label")
@Controller
public class UserLabelController {

    private static final Logger logger = LoggerFactory.getLogger(UserLabelController.class);

    @Autowired
    private UserLabelService userLabelService;
    @Autowired
    private UserRpc userRpc;

    @ResponseBody
    @RequestMapping(value = "/add")
    public Response add(long labelId, String userIdList){

        List<UserInfoBean> userList = new ArrayList<>();
        List<String> userIds = java.util.Arrays.asList(userIdList.split(","));
        for(String userId : userIds){
            Long id = Long.parseLong(userId);
            UserLabel userLabel = new UserLabel();
            userLabel.setUserId(id);

            UserInfoBean user = JSONObject.parseObject(userRpc.userInfo(id), UserInfoBean.class);
            userLabel.setLabelId(labelId);
            userLabel.setUser(user);
            userLabelService.add(userLabel);
            
            userList.add(user);
        }
        return Response.ok(userList);
    }

    @ResponseBody
    @RequestMapping(value = "/deleteByUserIdLabelId")
    public Response deleteByUserIdLabelId(Long userId, Long labelId){

        userLabelService.deleteByUserIdLabelId(userId, labelId);

        return Response.ok();
    }

    @ResponseBody
    @RequestMapping(value = "/findUserByLabelId")
    public Response findUser(UserLabelSearch userLabelSearch) {
        Long labelId = userLabelSearch.getLabelId();
        Integer page = userLabelSearch.getPage();
        Integer size = userLabelSearch.getSize();
        return Response.ok(userLabelService.findUserWithLabelId(labelId, page, size));
    }

    @ResponseBody
    @RequestMapping(value = "/findUserWithoutLabelId")
    public Response findUserWithoutLabelId(UserLabelSearch userLabelSearch){
        Long labelId = userLabelSearch.getLabelId();
        String nickName = userLabelSearch.getNickName();
        Integer page = userLabelSearch.getPage();
        Integer size = userLabelSearch.getSize();
        return userLabelService.findUserWithoutLabelId(labelId, nickName, page, size);
    }

}
